
package com.hlkj.pay.service.user.impl;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hlkj.framework.common.pojo.PageResult;
import com.hlkj.framework.common.provider.Aes256EncryptionProvider;
import com.hlkj.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.hlkj.pay.dto.user.UserQueryDto;
import com.hlkj.pay.infrastructure.mapper.user.UserMapper;
import com.hlkj.pay.infrastructure.model.user.UserDO;
import com.hlkj.pay.service.user.IUserService;

import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;

/**
 * @author HlpayTeam
 * @date 2024/10/25 14:47
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class UserServiceImpl implements IUserService {

    private final UserMapper userMapper;

    private final Aes256EncryptionProvider aes256EncryptionProvider;

    @Override
    // @Cacheable(value = RedisConstants.USER_INFO, key = "#phone", unless =
    // "#result == null")
    public UserDO queryUserByPhone(String phone) {
        String phoneQuery;
        try {
            phoneQuery = aes256EncryptionProvider.encryptString(phone);
        }
        catch (Exception e) {
            // throw new RuntimeException(e);
            phoneQuery = phone;
            log.error("queryUserByPhone error: message{}", e, e.getMessage());
        }
        return userMapper.selectOne(UserDO::getPhone, phoneQuery);
    }

    @Override
    // @CachePut(value = RedisConstants.USER_INFO, key = "#userDO.getPhone()",
    // unless = "#result == null")
    public UserDO saveUser(UserDO userDO) {
        try {
            String phoneDecrypt = aes256EncryptionProvider.encryptString(userDO.getPhone());
            userDO.setPhone(phoneDecrypt);
        }
        catch (Exception e) {
            log.error("saveUser error: message{}", e, e.getMessage());
        }
        userMapper.insert(userDO);
        return userDO;
    }

    @Override
    public PageResult<UserDO> queryPage(UserQueryDto userQueryDto) {
        if (StringUtils.hasText(userQueryDto.getPhone())) {
            try {
                String phoneDecrypt = aes256EncryptionProvider.encryptString(userQueryDto.getPhone());
                userQueryDto.setPhone(phoneDecrypt);
            }
            catch (Exception e) {
                log.error("saveUser error: message{}", e, e.getMessage());
            }
        }
        return userMapper.selectPage(userQueryDto, buildWrapper(userQueryDto));
    }

    @Override
    public UserDO queryUserById(Long id) {
        return userMapper.selectById(id);
    }

    @Override
    public void updateUser(UserDO userDO) {
        userMapper.updateById(userDO);
    }

    /**
     * 查询条件封装
     *
     * @param userQueryDto
     * @return
     */
    LambdaQueryWrapper<UserDO> buildWrapper(UserQueryDto userQueryDto) {
        LambdaQueryWrapperX<UserDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eqIfPresent(UserDO::getType, userQueryDto.getType());
        queryWrapper.eqIfPresent(UserDO::getStatus, userQueryDto.getStatus());
        queryWrapper.eqIfPresent(UserDO::getPhone, userQueryDto.getPhone());
        return queryWrapper;
    }
}
